home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / getgrent.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  246 lines

  1. RCS_ID_C="$Id: getgrent.c,v 2.1 1994/01/23 03:18:14 ppessi Exp $";
  2. /*
  3.  * Group database functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP usergroup.library.
  8.  *
  9.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Sun Nov 28 17:45:55 1993 ppessi
  13.  * Last modified: Sat Jan 22 16:10:36 1994 ppessi
  14.  *
  15.  * $Log: getgrent.c,v $
  16.  * Revision 2.1  1994/01/23  03:18:14  ppessi
  17.  * Updated the netinfo.device commands
  18.  *
  19.  * Revision 1.4  1994/01/21  08:13:47  ppessi
  20.  * Updated documentation
  21.  *
  22.  * Revision 1.3  1994/01/19  10:03:37  ppessi
  23.  * Added error checking
  24.  *
  25.  */
  26.  
  27. /****** usergroup.library/getgrent *******************************************
  28.  
  29.     NAME
  30.         getgrgid, getgrnam, getgrent, setgrent, endgrent
  31.          - group database operations
  32.  
  33.     SYNOPSIS
  34.         #include <grp.h>
  35.  
  36.         groupent = getgrgid(gid)
  37.            D0               D0
  38.         struct group *getgrgid(gid_t);
  39.  
  40.  
  41.         groupent = getgrnam(name)
  42.            D0                A1          
  43.         struct group *getgrnam(const char *);
  44.  
  45.  
  46.         groupent = getgrent()
  47.            D0
  48.         struct group *getgrent(void);
  49.  
  50.         setgrent()
  51.         void setgrent(void);
  52.  
  53.         endgrent()
  54.         void endgrent(void);
  55.  
  56.     FUNCTION
  57.         These functions operate on the group database via netinfo.device
  58.         interface. They provide a convenient unix-compatible interface to
  59.         the group unit of the netinfo.device.
  60.  
  61.         The local group database is stored in the file AmiTCP:db/group, its
  62.         format is described in netinfo.device/group.  The entry returned by
  63.         each reading function is defined by the structure group found in the
  64.         include file <grp.h>:
  65.  
  66.               struct group
  67.               {
  68.                 char   *gr_name;              \* Group name.  *\
  69.                 char   *gr_passwd;            \* Password.    *\
  70.                 gid_t   gr_gid;               \* Group ID.    *\
  71.                 char  **gr_mem;               \* Member list. *\
  72.                };
  73.  
  74.         The functions getgrnam() and getgrgid() search the group database
  75.         for the given group name pointed to by name or the group id pointed
  76.         to by gid, respectively, returning the first one encountered.
  77.         Identical group names or group gids may result in undefined
  78.         behavior.
  79.  
  80.         The getgrent() function sequentially reads the group database and is
  81.         intended for programs that wish to step through the complete list of
  82.         groups.
  83.  
  84.         All three routines will open the group unit of netinfo.device for
  85.         reading, if necesssary.
  86.  
  87.         The setgrent() function opens the group unit of netinfo.device.  The
  88.         endgrent() function closes the group unit of netinfo.device.  It is
  89.         recommended to call endgrent() if the program won't access group
  90.         database any more.
  91.  
  92.     RESULTS
  93.         The functions getgrent(), getgrnam(), and getgrgid(), return a
  94.         pointer to the group entry if successful; if the end of database is
  95.         reached or an error occurs a null pointer is returned.  The
  96.         functions endgrent() and setgrent() have no return value.
  97.  
  98.     ERRORS
  99.         [ENOENT] -- the netinfo.device could not be opened.
  100.  
  101.         Other netinfo.device IO errors can be retrieved by ug_GetErr().
  102.  
  103.     FILES
  104.         AmiTCP:db/group     The group database file
  105.  
  106.     SEE ALSO
  107.  
  108.         getpwnam(), netinfo.device/group
  109.  
  110.     HISTORY
  111.  
  112.         The functions getgrgid(), getgrnam(), getgrent(), setgrent() and
  113.         endgrent() appeared in Version 7 AT&T UNIX.
  114.  
  115.     BUGS
  116.  
  117.         These functions leave their results in an internal static object and
  118.         return a pointer to that object. Subsequent calls to the same
  119.         function will modify the same object. If you need re-entrant
  120.         operation, you can use directly the netinfo.device commands.
  121.  
  122.     COMPATIBILITY
  123.  
  124.         The BSD passwd database handling routines setgrfile() and
  125.         setgroupent() are fairly useless in a networked environment and they
  126.         are not implemented.
  127.  
  128. *****************************************************************************
  129. *
  130. */
  131.  
  132. #include "base.h"
  133. #include "libfunc.h"
  134.  
  135. static short done_set_ent = 0;
  136.  
  137. SAVEDS ASM void R_endgrent(void)
  138. {
  139.   ObtainSemaphore(ni_lock);
  140.   done_set_ent = 0;
  141.   CloseNIUnit(NETINFO_GROUP_UNIT);
  142.   ReleaseSemaphore(ni_lock);
  143. }
  144.  
  145. SAVEDS ASM void R_setgrent(void)
  146. {
  147.   struct NetInfoReq *nreq;
  148.  
  149.   ObtainSemaphore(ni_lock);
  150.  
  151.   if (nreq = OpenNIUnit(NETINFO_GROUP_UNIT)) {
  152.     nreq->io_Command = CMD_RESET;
  153.     myDoIO(nreq);
  154.     done_set_ent = 1;
  155.   } else {
  156.     SetDeviceErr();
  157.   }
  158.  
  159.   ReleaseSemaphore(ni_lock);
  160. }
  161.  
  162. SAVEDS ASM struct group *R_getgrent(void)
  163. {
  164.   struct NetInfoReq *nreq;
  165.   struct group *gr = NULL;
  166.  
  167.   ObtainSemaphore(ni_lock);
  168.   if (nreq = OpenNIUnit(NETINFO_GROUP_UNIT)) {
  169.  
  170.      /* do setgrent() if necessary */
  171.     if (!done_set_ent) {
  172.       nreq->io_Command = CMD_RESET;
  173.       myDoIO(nreq);
  174.       done_set_ent = 1;
  175.     }
  176.  
  177.     nreq->io_Command = CMD_READ;
  178.     if (myDoIO(nreq) == 0) {
  179.       gr = (struct group *)nreq->io_Data;
  180.     } else {
  181.       SetDeviceErr();
  182.     }
  183.   } else {
  184.     SetDeviceErr();
  185.   }
  186.  
  187.   ReleaseSemaphore(ni_lock);
  188.  
  189.   return gr;
  190. }
  191.  
  192. SAVEDS ASM struct group *R_getgrgid(REG(d0) gid_t gid)
  193. {
  194.   struct NetInfoReq *nreq;
  195.   struct group *gr = NULL;
  196.  
  197.   ObtainSemaphore(ni_lock);
  198.   if (nreq = OpenNIUnit(NETINFO_GROUP_UNIT)) {
  199.     gr = (struct group *)nreq->io_Data;
  200.     gr->gr_gid = gid;
  201.     nreq->io_Command = NI_GETBYID;
  202.  
  203.     if (myDoIO(nreq) != 0) {
  204.       gr = NULL;
  205.       SetDeviceErr();
  206.     }
  207.   } else {
  208.     SetDeviceErr();
  209.   }
  210.  
  211.   ReleaseSemaphore(ni_lock);
  212.  
  213.   return gr;
  214. }
  215.  
  216. SAVEDS ASM struct group *R_getgrnam(REG(a1) const char *name)
  217. {
  218.   struct NetInfoReq *nreq;
  219.   struct group *gr = NULL;
  220.  
  221.   if (name == NULL) {
  222.     SetErrno(EFAULT);
  223.     return NULL;
  224.   }
  225.  
  226.   ObtainSemaphore(ni_lock);
  227.   if (nreq = OpenNIUnit(NETINFO_GROUP_UNIT)) {
  228.  
  229.     gr = (struct group *)nreq->io_Data;
  230.     gr->gr_name = (char *)name;
  231.     nreq->io_Command = NI_GETBYNAME;
  232.  
  233.     if (myDoIO(nreq) != 0) {
  234.       gr = NULL;
  235.       SetDeviceErr();
  236.     }
  237.   } else {
  238.     SetDeviceErr();
  239.   }
  240.  
  241.   ReleaseSemaphore(ni_lock);
  242.  
  243.   return gr;
  244. }
  245.  
  246.